﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Kevin.SLUserControlLibrary
{
    public class ThemeColorsItem : ContentControl
    {
        public delegate void SelectComplete(ColorItem colorItem, SolidColorBrush selectedColor);
        public event SelectComplete OnSelectComplete;

        private KeyValuePair<Color, List<Color>> keyValuePair;

        private StackPanel colorsElement;
        private ColorItem keyColorItem;
        private List<ColorItem> colorItemList;

        public ThemeColorsItem(KeyValuePair<Color, List<Color>> keyValuePair, List<ColorItem> colorItemList)
        {
            this.DefaultStyleKey = typeof(ThemeColorsItem);
            this.keyValuePair = keyValuePair;
            this.Margin = new Thickness(2, 0, 2, 0);
            this.colorItemList = colorItemList;
        }

        public override void OnApplyTemplate()
        {
            this.keyColorItem = this.GetTemplateChild("KeyColorElement") as ColorItem;
            this.colorItemList.Add(this.keyColorItem);

            this.colorsElement = this.GetTemplateChild("ColorsElement") as StackPanel;

            this.keyColorItem.DisplayColor = new SolidColorBrush(this.keyValuePair.Key);
            this.keyColorItem.Click += new RoutedEventHandler(keyColorItem_Click);

            foreach (var info in keyValuePair.Value)
            {
                ColorItem item = new ColorItem();
                item.DisplayColor = new SolidColorBrush(info);
                item.BorderThickness = new Thickness(0, 0, 0, 0);
                item.Click += new RoutedEventHandler(keyColorItem_Click);
                this.colorItemList.Add(item);
                this.colorsElement.Children.Add(item);
            }
        }

        private void keyColorItem_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnSelectComplete != null)
            {
                ColorItem colorItem = sender as ColorItem;
                this.OnSelectComplete(colorItem, colorItem.DisplayColor);
            }
        }
    }
}
